//表单验证
$(function(){
	$('#customerBusinessName').focus(function () {
		//禁止弹出手机键盘
		document.activeElement.blur();
		$('#employeeListDiv').attr("style","display:block;");//显示div
	})
	$('#hiddenListBtn').click(function () {
		$('#employeeListDiv').attr("style","display:none;");//隐藏div
	})
	$('#clearBusinessBnt').click(function () {
		$('#customerBusinessId').val('');
		$('#customerBusinessName').val('');
	})
	
	//保存客户信息
	$('#saveCustomerBtn').click(function () {
		var customerId = $('#customerId').val();
		//获取验证器
		var customerName = $('#customerName').val();
		var linkName = $('#linkName').val();
		var mobile = $('#mobile').val();
		var password = $('#password').val();
		var repassword = $('#repassword').val();
		var customerType = $('input[name="customerType"]:checked').val();
		var email = $('#email').val();
		var customerLevel = $('#customerLevel').val();
		var customerDiscount = $('#customerDiscount').val();
		var weixin = $('#weixin').val();
		var weixinQrCodeImage = $('#weixinQrCodeImage').attr("src");
		var zhifubao = $('#zhifubao').val();
		var zhifubaoQrCodeImage = $('#zhifubaoQrCodeImage').attr("src");
		var qq = $('#qq').val();
		var sex = $('#sex').val();
		var idCard = $('#idCard').val();
		var address = $('#address').val();
		
		var customerBusinessId = $('#customerBusinessId').val();
		var customerBusinessName = $('#customerBusinessName').val();
		
		if(mobile == null || mobile == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(mobile)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		if(linkName == null || linkName == ""){
			layer.msg("请输入客户姓名或昵称!",{icon:2,time:2000});
			return;
		}
		if(linkName.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(customerId == null || customerId == "" || customerId == "0"){
			if(password == null || password == ""){
				layer.msg("密码不能为空!",{icon:2,time:2000});
				return;
			}
			if(!isCode(password)){
				layer.msg("亲,密码只能是英文字母和数字组合,不区分大小写哦!",{icon:2,time:2000});
				return;
			}
			if(password.length < 6){
				layer.msg("请输入至少6位密码!",{icon:2,time:2000});
				return;
			}
			if(repassword == null || repassword == ""){
				layer.msg("重复密码不能为空!",{icon:2,time:2000});
				return;
			}
			
			if(repassword != password){
				layer.msg("两次密码输入不一致!",{icon:2,time:2000});
				return;
			}
		}
		
		if(email != null && email != ""){
			email = email.trim();
			if(!isEmail(email)){
				layer.msg("邮箱不符合规范!",{icon:2,time:2000});
				return;
			}
		}
		if(weixin != null && weixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage != null && weixinQrCodeImage.length > 512){
			layer.msg("微信二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(qq != null && qq.length > 12){
			layer.msg("qq号不能超过12位!",{icon:2,time:2000});
			return;
		}
		
		if(idCard != null && idCard.length > 20){
			layer.msg("身份证号不能超过20位!",{icon:2,time:2000});
			return;
		}
		
		if(address != null && address.length > 512){
			layer.msg("地址不能超过512位!",{icon:2,time:2000});
			return;
		}
		var customer = {};
		customer["id"] = customerId;
		
		customer["customerName"] = customerName;
		customer["linkName"] = linkName;
		customer["pwd"] = password;
		customer["repwd"] = repassword;
		customer["email"] = email;
		customer["customerLevel"] = customerLevel;
		customer["customerDiscount"] = customerDiscount;
		customer["mobile"] = mobile;
		customer["type"] = customerType;
		customer["weixin"] = weixin;
		customer["weixinQrCode"] = weixinQrCodeImage;
		customer["zhifubao"] = zhifubao;
		customer["zhifubaoQrCode"] = zhifubaoQrCodeImage;
		customer["qq"] = qq;
		customer["sex"] = sex;
		customer["idCard"] = idCard;
		customer["address"] = address;
		
		customer["businessId"] = customerBusinessId;
		customer["businessName"] = customerBusinessName;
		
		$.ajax({
			url:baselocation+'/customer',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(customer),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/customer';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
});
function selectMyChoice(id,name){
	$('#customerBusinessId').val(id);
	$('#customerBusinessName').val(name);
	$('#employeeListDiv').attr("style","display:none;");//隐藏div
}
